import Vue from 'vue'
import Router from 'vue-router'
// 引入路由组件
import Tab from "@/pages/TabBar.vue"
import Home from "@/pages/Home.vue"
import House from "@/pages/House.vue"
import Collect from "@/pages/Collect.vue"
import My from "@/pages/My.vue"
import CityList from "@/pages/CityList"
import Detail from "@/pages/Detail"
import Login from "@/pages/Login"
import Registe from "@/pages/Registe"

Vue.use(Router)
const router=new Router({
  routes: [
    {
      path:"/",
      redirect:"/tab"// 重定向
    },
    {
      path: '/tab',
      name: 'tab',
      component: Tab,
      children:[
        {
          path:"/",// 父路由的根目录
          redirect:"/tab/home"// 重定向
        },
        {
          path:"home",
          name:"home",
          component:Home
        },
        {
          path:"house",
          name:"house",
          component:House
        },
        {
          path:"coll",
          name:"coll",
          component:Collect
        },
        {
          path:"my",
          name:"my",
          component:My
        },
      ]
    },
    {
      path:"/city",
      name:"city",
      component:CityList
    },
    {
      path:"/det",
      name:"det",
      component:Detail
    },
    {
      path:"/login",
      name:"login",
      component:Login
    },
    {
      path:"/registe",
      name:"registe",
      component:Registe
    }
  ]
})
// 全局路由钩子 拦截某些路由
// 设置黑名单 
  let hmd=["/det","/tab/coll"]
  // 判断即将进入的路由是否在黑名单数组 includes  indexOf

router.beforeEach((to, from, next) => {
        // console.log(to);// 即将进入的路由
        // console.log(from);// 即将离开的路由
        let islogin=localStorage.getItem("userinfo")
       if( hmd.includes(to.path)){
         if(islogin){
            next()// 登录成功放行
         }else{
           next("/login")
         }
       }else{
         next()

       }
  
})

export default router
